package com.book.web;

import com.book.pojo.Book;
import com.book.pojo.BookDetail;
import com.book.service.BookDetailService;
import com.book.service.BookService;
import com.book.service.impl.BookDetailServiceImpl;
import com.book.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BookDetailServlet extends BaseServlet {

    private BookDetailService bookDetailService = new BookDetailServiceImpl();
    private BookService bookService = new BookServiceImpl();

    protected void getDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取查询书籍的id
        int bookId = Integer.parseInt(req.getParameter("bookId"));

        //通过id查询相应信息
        Book book = bookService.queryBookById(bookId);
        BookDetail bookDetail = bookDetailService.queryBookDetailById(bookId);

        //判断book是否为null
        if(book == null){
            //直接跳回index
            resp.sendRedirect("index.jsp");
            return;
        }

        //将对应信息保存到request域中
        req.setAttribute("book", book);
        req.setAttribute("book_detail", bookDetail);

        //请求转发
        req.getRequestDispatcher("/pages/detail/detail.jsp").forward(req, resp);
    }

}
